package com.gao.web.service;

import com.gao.web.dao.UserMapper;
import com.gao.web.pojo.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/*
    专门处理用户相关业务
 */
@Service
public class UserService {
    @Autowired
    private UserMapper mapper;

    public int updateCustomer(int id, String username, String password, String name, String ID_number, String birth_date, int sex, String email, String address, String phone) {
        Customer customer = new Customer(id, username, password, name, ID_number, birth_date, sex, email, address, phone);
        return mapper.updateCustomer(customer);
    }

    public int insertCustomer(String username, String password, String name, int sex, String phone, String email, String address) {
        if (mapper.selectByName(username) != null) {
            return 0;
        }
        Customer customer = new Customer(username, password, name, sex, phone, email, address);
        return mapper.insertCustomer(customer);
    }

    public Customer finduser(String username) {
        return mapper.selectByName(username);
    }

    public int delete(int id) {
        Customer customer = mapper.selectById(id);
        return mapper.delete(customer);
    }

    public List<Customer> customerfindall() {
        return mapper.customerfindAll();
    }

    public List<Customer> findUsername(String username, String phone) {
        return mapper.findUsername(username, phone);
    }


    public Customer login(String username, String password) {
        System.out.println("正在处理用户登录业务：" + "[username:" + username + "] [password:" + password + "]");
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            return null;
        }
        Customer customer = new Customer(username, password);
        return mapper.findUsernameAndPassword(customer);
    }

    public int register(String username, String password, String phone) {
        System.out.println("正在处理用户注册业务：" + "[username:" + username + "] [password:" + password + "]");
        if (username == null || username.isEmpty() || password == null || password.isEmpty() || mapper.selectByName(username) != null) {
            return 0;
        }
        Customer customer = new Customer(username, password, phone);
        System.out.println(customer);
        return mapper.regis(customer);
    }

}

